package cn.dankal.flyaudio.adapter;

import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.DrawableRes;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.entity.Event;
import cn.dankal.flyaudio.entity.EventType;
import cn.dankal.flyaudio.friend.ChatActivity;
import cn.dankal.flyaudio.friend.utils.DialogCreator;
import cn.dankal.flyaudio.friend.utils.HandleResponseCode;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.content.TextContent;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.android.api.options.MessageSendingOptions;
import cn.jpush.im.api.BasicCallback;

public class ShareGroupAdapter extends BaseRecyclerViewAdapter<GroupInfo> {

    private String groupName;
    private boolean mIsForward;
    private int mWidth;
    private boolean mBusiness;
    private Dialog mLoadingDialog;
    private Dialog mDialog;
    private Map<Long, String> mGroupName = new HashMap<>();
    private String mUserName;
    private String mAppKey;
    private String mAvatarPath;

    public void initParams(boolean isFromForward, int width, boolean isBusinessCard, String userName, String appKey, String path) {
        this.mIsForward = isFromForward;
        this.mWidth = width;
        this.mBusiness = isBusinessCard;
        this.mUserName = userName;
        this.mAppKey = appKey;
        this.mAvatarPath = path;
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_group_list;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    private String getGroupName(List<UserInfo> groupMembers, StringBuilder builder) {
        for (UserInfo info : groupMembers) {
            String noteName = info.getDisplayName();
            builder.append(noteName);
            builder.append(",");
        }

        return builder.substring(0, builder.lastIndexOf(","));
    }

    public class ViewHolder extends BaseRecyclerViewHolder<GroupInfo> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(GroupInfo data, int position) {
            if (TextUtils.isEmpty(data.getGroupName())) {
                //Conversation groupConversation = JMessageClient.getGroupConversation(groupId);
                //群组名是null的话,手动拿出5个名字拼接
                List<UserInfo> groupMembers = data.getGroupMembers();

                StringBuilder builder = new StringBuilder();
                if (groupMembers.size() <= 5) {
                    groupName = getGroupName(groupMembers, builder);
                } else {
                    List<UserInfo> newGroupMember = groupMembers.subList(0, 5);
                    groupName = getGroupName(newGroupMember, builder);
                }
            } else {
                groupName = data.getGroupName();
            }

            mGroupName.put(data.getGroupID(), groupName);
            setText(R.id.group_name, groupName);
            data.getAvatarBitmap(new GetAvatarBitmapCallback() {
                @Override
                public void gotResult(int i, String s, Bitmap bitmap) {
                    if (i == 0) {
                        setImageBitmapRes(R.id.group_iv, bitmap);
                    } else {
                        setImageUrl(R.id.group_iv, getResourcesUri(R.drawable.group));
                    }
                }
            });
//            itemView.findViewById(R.id.group_ll).setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            });
        }
    }

    private String getResourcesUri(@DrawableRes int id) {
        Resources resources = context.getResources();
        String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                resources.getResourcePackageName(id) + "/" +
                resources.getResourceTypeName(id) + "/" +
                resources.getResourceEntryName(id);
        return uriPath;
    }
}
